package com.example.loglib;

import android.util.Log;

import com.example.loglib.Proxy.LogI;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by huangbei on 2017/8/28.
 */

//显示log
public class LogUtil implements LogI{
    private static LogUtil instance;

    private Config config;

    private LogUtil(){
        config = Config.getInstance();
    }

    public static LogUtil getInstance(){
        if(instance == null){
            synchronized(Config.class){
                if(instance == null){
                    instance = new LogUtil();
                }
            }
        }
        return instance;
    }

    public Config getConfig(){
        return this.config;
    }


    private boolean isShowTag(String tag , int catgory_tag){
        boolean show = config.isShow()&&config.isShowThisTag(tag);
        switch (catgory_tag){
            case Constant.E_TAG:
                show = show && config.isShowE();
                break;
            case Constant.W_TAG:
                show = show && config.isShowW();
                break;
            case Constant.I_TAG:
                show = show && config.isShowI();
                break;
            default:
                break;
        }
        return show;
    }

    public void elog(String content){
        elog(getClassName(),content);
    }

    public void elog(String tag , String content){
        if(!isShowTag(tag,Constant.E_TAG)){
            return;
        }
        if(config.isShowE()){
            Log.e(config.getMyTag()+" "+tag,getTime()+" "+content);
        }
    }

    public void ilog(String content){
        ilog(getClassName(),content);
    }

    public void ilog(String tag , String content){
        if(!isShowTag(tag,Constant.I_TAG)){
            return;
        }
        if(config.isShowI()){
            Log.i(config.getMyTag()+" "+tag,getTime()+" "+content);
        }
    }

    public void wlog(String content){
        wlog(getClassName(),content);
    }

    public void wlog(String tag , String content){
        if(!isShowTag(tag,Constant.W_TAG)){
            return;
        }
        if(config.isShowW()){
            Log.w(config.getMyTag()+" "+tag,getTime()+" "+content);
        }
    }

    public String getTime(){
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        return time;
    }

    //获取调用者的类名
    public String getClassName(){
        StackTraceElement stack[] = (new Throwable()).getStackTrace();
//        for (int i = 0; i < stack.length; i++)
//        {
//            StackTraceElement s = stack[i];
//            System.out.format(" ClassName:%d\t%s\n", i, s.getClassName());
//            System.out.format("MethodName:%d\t%s\n", i, s.getMethodName());
//            System.out.format("  FileName:%d\t%s\n", i, s.getFileName());
//            System.out.format("LineNumber:%d\t%s\n\n", i, s.getLineNumber());
//        }
        if(stack != null && stack.length > 2) {
            return stack[2].getClassName();
        }else{
            return "";
        }
    }
}
